//
//  ZJLocalDataManager.m
//  Project
//
//  Created by 郑键 on 17/1/24.
//  Copyright © 2017年 zhengjian. All rights reserved.
//

#import "ZJLocalDataManager.h"
/**
 *  第三方&系统库
 */
#import <FMDB/FMDB.h>

@interface ZJLocalDataManager ()
{
    FMDatabase *_db;
}

@end

@implementation ZJLocalDataManager

#pragma mark - 初始化

/**
 获取单利
 
 @return 对象
 */
+ (instancetype)sharedLocalDataManager
{
    static dispatch_once_t onceToken;
    static ZJLocalDataManager *_manager;
    dispatch_once(&onceToken, ^{
        _manager                            = [[ZJLocalDataManager alloc] init];
        [_manager createDataBase];
        [_manager creatTable];
    });
    return _manager;
}

/**
 创建数据库
 */
- (void)createDataBase
{
    NSString *doc                           = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) firstObject];
    NSString *filePath                      = [doc stringByAppendingPathComponent:@"contacts.sqlite"];
    _db                                     = [FMDatabase databaseWithPath:filePath];
}

/**
 创建表
 */
- (void)creatTable
{
    if ([_db open]) {
        BOOL result                         = [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_system (id integer PRIMARY KEY AUTOINCREMENT, currentSystemVersion text NOT NULL, severCurrentVersionPatchBuild text NOT NULL);"];
        if (result) {
            NSLog(@"创建成功");
        }else{
            NSLog(@"创建失败");
        }
        [_db close];
    }else{
        NSLog(@"数据库打开失败");
    }
}

#pragma mark - 数据库操作

/**
 插入数据操作

 @param contact 数据模型
 */
- (void)insertContact:(id)contact
{
    if ([_db open]) {
        BOOL result = [_db executeUpdate:@"INSERT INTO t_system (currentSystemVersion, severCurrentVersionPatchBuild) VALUES (?,?);", @"2.0", @"2"];
        if (result) {
            NSLog(@"创建成功");
        }else{
            NSLog(@"创建失败");
        }
        [_db close];
    }else{
        NSLog(@"数据库打开失败");
    }
}

@end
